import {login,getRoles} from '@/api/users';
import {setCookie} from '@/utils/auth';
const state ={
    username:'',
    token:'',
    roles:[]
};
const mutations ={
    'SET_USERNAME'(state,username){
        state.username = username
    },
    'SET_TOKEN'(state,token){
        state.token = token
    },
    'SET_ROLES'(state,roles){
        state.roles = roles
    }
};
const actions = {
    login({commit},{username,password}){
       return new Promise((resolve,reject)=>{
            login({username,password}).then(response=>{
                const {token,username} = response.data;
                commit('SET_USERNAME',username);
                commit('SET_TOKEN',token);
                setCookie(token);
                resolve(response)
            })
       })
    },
    getRoles({commit}){
        return new Promise((resolve,reject)=>{
            getRoles().then(response=>{
                const {roles} = response;
                commit('SET_ROLES',roles)
                resolve(response)
            })
        })
    }
}


export default {
    namespaced:true,
    state,
    mutations,
    actions
}